package com.threeonemeeting.task;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

import com.snda.lib.http.HttpUtil;
import com.snda.lib.http.ICallBack;
import com.threeonemeeting.Const;
import com.threeonemeeting.model.MeetingDataDocInfo;

public class GetDocumentsImageListTask extends ApplicationTask {

	public GetDocumentsImageListTask(Context context, ICallBack callback) {
		super(context, callback);

		this.nTaskType = HttpConst.TASKTYPE_GETDOCUMENTIMAGELIST;
		this.strUrl = "http://new.31huiyi.com/json/GetDocListHandler.ashx";
	}

	// 具体的task实现该方法，对响应报文进行解析
	protected Map<String, Object> parseResponse(String strResponse) {

		Map<String, Object> mapData = new HashMap<String, Object>();
		mapData.put(HttpConst.KEY_TASK_TYPE, nTaskType);
		mapData.put(HttpUtil.KEY_ERROR_CODE, HttpUtil.ERROR_SUCCESS);

		JSONObject jsonResp = null;
		try {
			jsonResp = new JSONObject(strResponse);
		} catch (Exception e) {
			setErr(HttpUtil.ERROR_SERVER_PACKET, mapData);
			return mapData;
		}

		try {
			// 获取 "Setting"部分的内容
			String strSuccess = jsonResp.getString("success");
			if (strSuccess == null) {
				setErr(HttpUtil.ERROR_SERVER_PACKET, mapData);
				return mapData;
			}

			if (strSuccess.equalsIgnoreCase("true") == false) {
				setErr(HttpUtil.ERROR_SERVER_PACKET, mapData);
				return mapData;
			}

			JSONArray dataJson = jsonResp.getJSONArray("data");
			if (dataJson == null) {
				setErr(HttpUtil.ERROR_SERVER_PACKET, mapData);
				return mapData;
			}

			List<MeetingDataDocInfo> listDocuments = new ArrayList<MeetingDataDocInfo>();

			for (int i = 0, n = dataJson.length(); i < n; i++) {

				JSONObject jData = dataJson.getJSONObject(i);

				try {

					MeetingDataDocInfo docInfo = new MeetingDataDocInfo();
					docInfo.nDocId = jData.getInt("DocId");
					docInfo.nMeetingId = jData.getInt("EventId");
					docInfo.strFileTitle = jData.getString("Title");
					docInfo.strFileName = jData.getString("FileName");
					docInfo.strFileType = jData.getString("FileType");
					docInfo.strFileUrl = jData.getString("FullPath");
					docInfo.nFileSize = jData.getInt("FileSize");
					docInfo.nViewCount = jData.getInt("ViewCount");
					docInfo.nUserId = jData.getInt("UserId");
					docInfo.strUserName = jData.getString("UserName");
					docInfo.bIsSuggest = jData.getBoolean("IsSuggest");
					docInfo.strFileTime = jData.getString("AddTime");
					docInfo.nStatus = jData.getInt("Status");

					listDocuments.add(docInfo);

				} catch (JSONException ex) {
					Log.e(Const.Tag, "FullMeeting Info error");
				}
			}

			mapData.put("list", listDocuments);
			setErr(HttpUtil.ERROR_SUCCESS, mapData);
			return mapData;

		} catch (JSONException ex) {
			setErr(HttpUtil.ERROR_SERVER_PACKET, mapData);
			return mapData;
		}
	}
}
